package com.dtguai.web.util.redis;

import com.alibaba.fastjson.JSON;
import com.dtguai.web.base.service.BaseService;
import com.dtguai.web.common.cache.CacheKeys;
import com.dtguai.web.page.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author guoLiang
 * @date 2018-09-29 03:46:03
 */
@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;

    public static final int TIME_OUT_DEF = 60 * 60 * 2;

    public static final int TIME_OUT_DAY = 60 * 60 * 24;

    private static final String DEFAULT_CACHE_KEY = ":"+CacheKeys.PROJECT_NAME+"cache";

    private String getCacheKeyHash(PageModel pm) {
        return pm.getClass().getName() + DEFAULT_CACHE_KEY + ":hash" + ":id:" + pm.getId();
    }

    private String getCacheKeySet(PageModel pm) {
        return pm.getClass().getName() + DEFAULT_CACHE_KEY + ":set" + ":id:" + pm.getId();
    }

    private String getCacheKeyList(PageModel pm) {
        return pm.getClass().getName() + DEFAULT_CACHE_KEY + ":list" + ":id:" + pm.getId();
    }

    private String getCacheKeyZset(PageModel pm) {
        return pm.getClass().getName() + DEFAULT_CACHE_KEY + ":zset" + ":id:" + pm.getId();
    }

    private String getCacheKeyString(PageModel pm) {
        return pm.getClass().getName() + DEFAULT_CACHE_KEY + ":string" + ":id:" + pm.getId();
    }


    public <T> T getModel(String key, Class<T> clazz) {
        String json = redisTemplate.opsForValue().get(key);
        return JSON.parseObject(json, clazz);
    }

    public String getString(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public PageModel getModel(PageModel pm) {
        String json = redisTemplate.opsForValue().get(getCacheKeyString(pm));
        return JSON.parseObject(json, pm.getClass());
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * PageModel setPageModel
     */
    public void setPageModel(PageModel pm) {
        setString(getCacheKeyString(pm), JSON.toJSONString(pm));
    }

    /**
     * String set 系列
     */
    public void setString(String key, String value) {
        setString(key, value, TIME_OUT_DEF);
    }

    /**
     * 插入缓存
     * @param key key
     * @param value 字符串内容
     * @param time 时间单位:秒
     */
    public void setString(String key, String value, Integer time) {
        setString(key, value, time, TimeUnit.SECONDS);
    }

    public void setString(String key, String value, Integer time, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, time, timeUnit);
    }


    /**
     * hash get 系列
     */
    public Object getHash(String hashKey, String key) {
        return hashOperations.get(hashKey, key);
    }

    /**
     * hash set 系列
     */
    public void setHash(String collectionKey, Object key, Object value, Integer time) {
        redisTemplate.opsForHash().put(collectionKey, key.toString(), JSON.toJSONString(value));
        redisTemplate.expire(collectionKey, TIME_OUT_DEF, TimeUnit.SECONDS);
    }

    public PageModel getModel(Integer id, PageModel pm, BaseService service) {
        if (null == pm) {
            pm = (PageModel) service.selectByKey(id);
            if (null != pm) {
                setPageModel(pm);
            }
        }
        return pm;
    }
}